﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using RayD.Core;
using RayD.Repository.Entites.SystemModule;
using RayD.App.SystemModule;
using RayD.WebApi.Models;
using RayD.Repository.Core;

namespace RayD.WebApi.Controllers.Admin
{
    /// <summary>
    /// 权限资源
    /// </summary>
    public class AuthorizeController : BaseAdminController
    {
        #region 构造函数注入
        private AuthorizeApp _Authorize;
        public AuthorizeController(AuthorizeApp Authorize)
        {
            this._Authorize = Authorize;
        }
        #endregion

        #region 参数实体
        /// <summary>
        /// 获取按钮树型请求参数
        /// </summary>
        public class GetButtonTreeRequest
        {
            /// <summary>
            /// 对象主键
            /// </summary>
            public string objectId { get; set; }
            /// <summary>
            /// 模块主键集
            /// </summary>
            public List<string> moduleIds { get; set; }
        }
        /// <summary>
        /// 获取树型响应参数
        /// </summary>
        public class GetTreeResponse
        {
            /// <summary>
            /// 树型数据
            /// </summary>
            public List<TreeModel> TreeList { get; set; }
            /// <summary>
            /// 已勾选数据
            /// </summary>
            public List<string> CheckedAuthorities { get; set; }
        }

        /// <summary>
        /// 权限数据响应参数
        /// </summary>
        public class GetAuthResponse
        {
            /// <summary>
            /// 权限模块
            /// </summary>
            public IEnumerable<ModuleEntity> MenuList { get; set; }
            /// <summary>
            /// 权限按钮
            /// </summary>
            public List<ModuleButtonEntity> ButtonList { get; set; }
        }
        /// <summary>
        /// 保存授权数据请求参数
        /// </summary>
        public class SaveFormRequest
        {
            /// <summary>
            /// 权限分类-1角色2用户3岗位
            /// </summary>
            public int objectType { get; set; }
            /// <summary>
            /// 对象主键
            /// </summary>
            public string objectId { get; set; }
            /// <summary>
            /// 模块主键集
            /// </summary>
            public List<string> moduleIds { get; set; }
            /// <summary>
            /// 按钮主键集
            /// </summary>
            public List<string> moduleButtonIds { get; set; }
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取权限数据（菜单 + 按钮）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<GetAuthResponse> GetAuthList()
        {
            var userInfo = AuthenticationHelper.LoginUserInfo;
            //加载系统菜单
            List<ModuleEntity> moduleList = _Authorize.GetAuthorizeModuleList(userInfo.account);
            //加载菜单按钮
            List<ModuleButtonEntity> buttonList = new List<ModuleButtonEntity>();
            if (!moduleList.IsEmpty())
            {
                buttonList = _Authorize.GetAuthorizeButtonList(userInfo.account);
            }
            return Success(new GetAuthResponse()
            {
                MenuList = moduleList.ToTree(),
                ButtonList = buttonList
            });
        }


        /// <summary>
        /// 获取授权功能树型数据
        /// </summary>
        /// <param name="objectId">对象主键</param>
        /// <returns></returns>
        [HttpGet]
        public ResParameter<GetTreeResponse> GetModuleTreeList(string objectId)
        {
            var userInfo = AuthenticationHelper.LoginUserInfo;
            //加载系统菜单
            var moduleList = _Authorize.GetAuthorizeModuleList(userInfo.account);

            #region 树形转化
            List<TreeModel> treeList = new List<TreeModel>();
            if (!moduleList.IsEmpty())
            {
                foreach (var module in moduleList)
                {
                    TreeModel node = new TreeModel();
                    node.id = module.B_ModuleId;
                    node.value = module.B_ModuleId;
                    node.label = module.B_FullName;
                    node.parentId = module.B_ParentId;
                    node.isLeaf = module.B_ParentId.IsEmpty() ? false : true;
                    treeList.Add(node);
                }
            }
            #endregion

            #region 获取对象授权功能主键
            List<string> CheckedAuthorities = _Authorize.GetItemIdList(objectId, 1);
            #endregion

            return Success(new GetTreeResponse()
            {
                TreeList = treeList.ToTree(),
                CheckedAuthorities = CheckedAuthorities
            });
        }
        /// <summary>
        /// 获取授权按钮树型数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResParameter<GetTreeResponse> GetButtonTreeList(GetButtonTreeRequest request)
        {
            var userInfo = AuthenticationHelper.LoginUserInfo;

            #region 模块功能、模块按钮列表获取
            var rawModuleList = _Authorize.GetAuthorizeModuleList(userInfo.account);

            List<ModuleEntity> moduleList = new List<ModuleEntity>();
            for (int i = 0; i < request.moduleIds.Length(); i++)
            {
                moduleList.Add(rawModuleList.Find(c => c.B_ModuleId.Equals(request.moduleIds[i])));
            }
            #endregion

            #region 树形转化
            List<TreeModel> treeList = new List<TreeModel>();
            if (!moduleList.IsEmpty())
            {
                foreach (var module in moduleList)
                {
                    TreeModel node = new TreeModel();
                    node.id = module.B_ModuleId;
                    node.value = module.B_ModuleId;
                    node.label = module.B_FullName;
                    node.parentId = module.B_ParentId;
                    node.isLeaf = false;
                    treeList.Add(node);

                    var buttonList = _Authorize.GetAuthorizeButtonList(userInfo.account, module.B_ModuleId);
                    if (!buttonList.IsEmpty())
                    {
                        foreach (var button in buttonList)
                        {
                            TreeModel buttonNode = new TreeModel();
                            buttonNode.id = button.B_ModuleButtonId;
                            buttonNode.value = button.B_ModuleButtonId;
                            buttonNode.label = button.B_FullName;
                            buttonNode.parentId = button.B_ModuleId;
                            buttonNode.isLeaf = true;
                            buttonNode.icon = "el-icon-s-promotion";
                            treeList.Add(buttonNode);
                        }
                    }

                }
            }
            #endregion

            #region 获取对象授权功能主键
            List<string> CheckedAuthorities = _Authorize.GetItemIdList(request.objectId, 2);
            #endregion

            return Success(new GetTreeResponse()
            {
                TreeList = treeList.ToTree(),
                CheckedAuthorities = CheckedAuthorities
            });
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 保存授权数据
        /// </summary>
        /// <returns></returns>
        [HttpPut, Logging("授权数据保存")]
        public ResParameter SaveForm(SaveFormRequest request)
        {
            var (isSuccess, msg) = _Authorize.SaveAuthorize(request.objectType, request.objectId, request.moduleIds.ToArray(), request.moduleButtonIds.ToArray());
            if (isSuccess)
            {
                return Success(msg);
            }
            else
            {
                return Fail(msg);
            }
        }
        #endregion
    }
}
